package cn.fintecher.gis.service;

import cn.fintecher.gis.entity.LogInfo;
import cn.fintecher.gis.model.session.SessionModel;
import cn.fintecher.gis.respository.LogInfoRepository;
import cn.fintecher.gis.utils.ReadProperties;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.Objects;

@Service("logInfoService")
public class LogInfoService {
    private static final Logger log = LoggerFactory.getLogger(LogInfoService.class);

    @Autowired
    private LogInfoRepository logInfoRepository;

    @Autowired
    private OperatorService operatorService;


    /**
     * 日志的添加
     * @param className
     * @param methodName
     * @param param
     * @param describe
     */
    public void addLog(String className, String methodName, String param, String describe) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String accessToken = request.getHeader("X-OperatorToken");
        SessionModel operator = operatorService.findOperatorByToken(accessToken);
        try {
            if (Objects.nonNull(operator)) {
                LogInfo logInfo = new LogInfo();
                logInfo.setClassName(className);
                logInfo.setMethodName(methodName);
                logInfo.setMethodType(request.getMethod());
                logInfo.setRecordTime(new Date());
                logInfo.setUserName(operator.getUsername());
                logInfo.setVisitIp(request.getRemoteAddr());
                logInfo.setRemark(describe);
                logInfo.setParam(param);
                logInfoRepository.save(logInfo);
            }
        } catch (Exception e) {
            log.error("保存日志出错：" + e.getMessage());
        }
    }

    public void addLog(JoinPoint joinPoint) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String accessToken = request.getHeader("X-OperatorToken");
        SessionModel operator = operatorService.findOperatorByToken(accessToken);
        // 记录下请求内容
        log.info("URL : " + request.getRequestURL().toString());
        String className = joinPoint.getSignature().getDeclaringTypeName();
        String methodName = joinPoint.getSignature().getName();
        String param = Arrays.toString(joinPoint.getArgs());
        try {
            if (Objects.nonNull(operator)) {
                String describe = ReadProperties.getMethodName(methodName);
                if (StringUtils.isNotEmpty(describe)) {
                    LogInfo logInfo = new LogInfo();
                    logInfo.setClassName(className);
                    logInfo.setMethodName(methodName);
                    logInfo.setMethodType(request.getMethod());
                    logInfo.setRecordTime(new Date());
                    logInfo.setUserName(operator.getUsername());
                    logInfo.setVisitIp(request.getRemoteAddr());
                    logInfo.setRemark(describe);
                    if(!methodName.endsWith("login")){
                        logInfo.setParam(param);
                    }
                    logInfoRepository.save(logInfo);
                }
            }
        } catch (Exception e) {
            log.error("保存日志出错：" + e.getMessage());
        }
    }
}
